package cn.yunhe.validate;

import cn.yunhe.entity.Transaction;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class TransactionValidate implements Validator {
    /**
     * 对对应的实体类开启校验
     *
     * @param aClass
     * @return
     */
    @Override
    public boolean supports(Class<?> aClass) {
        return Transaction.class.equals(aClass);
    }

    @Override
    public void validate(Object o, Errors errors) {
        Transaction transaction = (Transaction) o;

        Double s = transaction.getAmount() - (transaction.getPrice() * transaction.getQuantity());
        if (Math.abs(s) > 0.01) {
            errors.rejectValue("amount", null, "交易金额有误");
        }
    }
}
